//////////////////////////////////////////////////////////////////////////
//
// GAM203
// Render Window
// RenderWindow.cpp
// 
// Grant Johnson
// Qantm College
// 27/10/2010
//
//////////////////////////////////////////////////////////////////////////

#include "RenderWindow.h"
#include "InputMgr.h"
#include "SoundMgr.h"
#include "Renderer.h"
#include "DXMgr.h"
#include "World.h"

#include "MainWindow.h"
#include "LeftChild.h"
#include "MiddleChild.h"
#include "RightChild.h"

#include <commctrl.h>

//Window Splitter
#define LEFT_WINDOW_WIDTH	200
#define RIGHT_WINDOW_WIDTH	245
#define TOOLBAR_HEIGHT		26
#define STATUSBAR_HEIGHT	26

#define LEFT_CLASS_NAME		"Left Window"
#define MIDDLE_CLASS_NAME	"Middle Window"
#define WINDOW_CLASS_NAME	"Main Window"
#define RIGHT_CLASS_NAME	"Right Window"

RenderWindow::RenderWindow()
{
	m_pMainWindow = new MainWindow();
	m_pLeftChild = new LeftChild();
	m_pMiddleChild = new MiddleChild();
	m_pRightChild = new RightChild();
}
RenderWindow::~RenderWindow()
{
	if(m_pMainWindow)
	{
		delete m_pMainWindow;
		m_pMainWindow = NULL;
	}
	if(m_pLeftChild)
	{
		delete m_pLeftChild;
		m_pLeftChild = NULL;
	}
	if(m_pMiddleChild)
	{
		delete m_pMiddleChild;
		m_pMiddleChild = NULL;
	}
	if(m_pRightChild)
	{
		delete m_pRightChild;
		m_pRightChild = NULL;
	}
}

bool RenderWindow::Setup(char* a_windowName, int a_width, int a_height, HINSTANCE a_hInst)
{
	InitCommonControls();

	//Create the windows
	m_pMainWindow->Setup(NULL, a_hInst, 0, 0, a_width, a_height, a_windowName, WINDOW_CLASS_NAME, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN);
	
	RECT rect;
	GetClientRect(m_pMainWindow->GetWindow(), &rect);

	m_pLeftChild->Setup(m_pMainWindow->GetWindow(), a_hInst, rect.left, rect.top + TOOLBAR_HEIGHT, LEFT_WINDOW_WIDTH, (rect.bottom - rect.top) - STATUSBAR_HEIGHT - TOOLBAR_HEIGHT, "", LEFT_CLASS_NAME, WS_CHILD | WS_VISIBLE);
	m_pMiddleChild->Setup(m_pMainWindow->GetWindow(), a_hInst, rect.left + LEFT_WINDOW_WIDTH, rect.top + TOOLBAR_HEIGHT, rect.right - (rect.left + LEFT_WINDOW_WIDTH + RIGHT_WINDOW_WIDTH), (rect.bottom - rect.top) - STATUSBAR_HEIGHT - TOOLBAR_HEIGHT, "", MIDDLE_CLASS_NAME, WS_CHILD | WS_VISIBLE);
	
	m_pRightChild->Setup(m_pMainWindow->GetWindow(), a_hInst, rect.left + LEFT_WINDOW_WIDTH + (rect.right - (rect.left + LEFT_WINDOW_WIDTH + RIGHT_WINDOW_WIDTH)), rect.top + TOOLBAR_HEIGHT, RIGHT_WINDOW_WIDTH, (rect.bottom - rect.top) - STATUSBAR_HEIGHT - TOOLBAR_HEIGHT, "", RIGHT_CLASS_NAME, WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN);

	//Setup the Direct3D manager
	if(!DXMgr::GetInstance().Setup(m_pMiddleChild->GetWindow()))
	{
		MessageBox(NULL, "Failed to Initialize Direct3D!", "Fail!", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}
	//Setup the input manager
	if(FAILED(InputMgr::GetInstance().Setup(a_hInst, m_pMainWindow->GetWindow())))
	{
		MessageBox(NULL, "Failed to Initialize Direct Input!", "Fail!", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}
	//Setup the renderer
	GetClientRect(m_pMiddleChild->GetWindow(), &rect);
	if(FAILED(Renderer::GetInstance().Setup(DXMgr::GetInstance().GetDevice(), rect.right, rect.bottom)))
	{
		MessageBox(NULL, "Failed to Initialize the Renderer!", "Fail!", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}
	//Setup the sound manager
	if(FAILED(SoundMgr::GetInstance().Setup()))
	{
		MessageBox(NULL, "Failed to Initialize the Sound Manager!", "Fail!", MB_OK | MB_ICONEXCLAMATION);
		return 0;
	}

	//Setup the game world
	World::GetInstance().Setup(&Renderer::GetInstance());

	return true;
}

void RenderWindow::Update()
{
	m_pMainWindow->Update();
	m_pLeftChild->Update();
	m_pMiddleChild->Update();
	m_pRightChild->Update();
}
float RenderWindow::GetDeltaTime()
{
	return m_timer.GetFrameTime();
}